package com.qy.base.utils.kuaizuyunSMS;

import java.util.ArrayList;
import java.util.List;

import com.qy.base.core.Constants;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

//HTTPS版本短信发送DEMO
public class KuaiZuYunSMS {

    private static HttpClient httpclient;

    public static Boolean sendSMS(String phone, String content, Integer no) throws Exception {
        httpclient = new SSLClient();
        String text = "";
        if (no == 1) {
            text = "【双创中国】您的验证码是" + content + "。如非本人操作，请忽略本短信。该验证码将在30分钟后失效。";
        } else if (no == 2) {
            text = "【双创中国】您注册的" + content + "基地会员已通过后台审核，初始密码为123456，请您及时登录进行修改。";
        } else if (no == 3) {
            text = "【双创中国】您注册的" + content + "基地会员未通过后台审核，请您核对信息后重新注册。";
        } else if (no == 4) {
            text = "【政策小管家】您的验证码是" + content + "。如非本人操作，请忽略本短信。该验证码将在30分钟后失效。";
        } else if (no == 5) {
            text = "【政策小管家】您的提现申请成功，请注意查收。";
        } else if (no == 6) {
            text = "【政策小管家】您的提现申请不成功，请重新提交。";
        }
        HttpPost post = new HttpPost(Constants.ACCESS_URL);
        post.setHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8");
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("action", Constants.SEND_ACTION));
        nvps.add(new BasicNameValuePair("userid", ""));
        nvps.add(new BasicNameValuePair("account", Constants.ACCOUNT_NAME));
        nvps.add(new BasicNameValuePair("password", Constants.PASSWORD));
        nvps.add(new BasicNameValuePair("mobile", phone));        //多个手机号用逗号分隔
        nvps.add(new BasicNameValuePair("content", text));
        nvps.add(new BasicNameValuePair("sendTime", ""));
        nvps.add(new BasicNameValuePair("extno", ""));

        post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

        HttpResponse response = httpclient.execute(post);

//        System.out.println(response.getStatusLine());
        HttpEntity entity = response.getEntity();
        // 将字符转化为XML
        String returnString = EntityUtils.toString(entity, "UTF-8");
        Document doc = DocumentHelper.parseText(returnString);
        // 获取根节点
        Element rootElt = doc.getRootElement();
        // 获取根节点下的子节点的值
        String returnstatus = rootElt.elementText("returnstatus").trim();
        String message = rootElt.elementText("message").trim();
        String remainpoint = rootElt.elementText("remainpoint").trim();
        String taskID = rootElt.elementText("taskID").trim();
        String successCounts = rootElt.elementText("successCounts").trim();

//        System.out.println(returnString);
//        System.out.println("返回状态为：" + returnstatus);
//        System.out.println("返回信息提示：" + message);
//        System.out.println("返回余额：" + remainpoint);
//        System.out.println("返回任务批次：" + taskID);
//        System.out.println("返回成功条数：" + successCounts);
        EntityUtils.consume(entity);
        if (returnstatus.equals("Success")) {
            return true;
        } else {
            return false;
        }
    }

}
